// src/crudOption/service/serviceList.js
export default () => {
  // ===== 搜索 =====
  const searchOption = ref({
    formConfig: {
      categoryName: { label: "类目名称", component: "el-input" },
      createTime: {
        label: "新增时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
        unlinkPanels: true,
      },
      updateTime: {
        label: "编辑时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
        unlinkPanels: true,
      },
      updateName: {
        label: "操作人员",
        component: "el-input",
      },
      // 字典：展示状态（如果后端暂未返回，可只用作搜索条件）
      displayStatus: {
        label: "展示状态",
        component: "el-select",
        clearable: true,
        filterable: true,
        placeholder: "全部",

        dicUrl: "/support-backend/dict/listChildrenByDictCode",
        dicQuery: { dictCode: "qingyun.support.serviceCenter.dispalyStatus" }, // 按文档拼写
        dicFormatter: res => {
          const list = Array.isArray(res.data) ? res.data : [];
          return list.map(i => ({ label: i.dictLabel, value: i.dictValue }));
        },

        // 这里就用默认的 label/value（因为上面已映射）
        dicProps: { label: "label", value: "value" },
      },
    },
  });

  // ===== 表格/表单 =====
  const tableOption = ref(
    Utils.tools.deleteSearchConfig({
      addTitle: "新增服务类目",
      editTitle: "编辑服务类目",
      dialogWidth: "30%", // 20% 在窄屏可能太小，这里给个像素更稳；按需改回

      // 按钮文字配置
      saveBtnText: "确定新增", // 保存按钮文字

      page: false, // 树表不分页
      stripe: false,
      align: "left",
      menu: true,
      menuWidth: 200,
      addBtn: false,
      editBtn: false,
      delBtn: false,

      // 树配置
      rowKey: "id",
      tree: true,
      defaultExpandAll: false,
      children: "children",
      cancelBtn: false, // 只有保存按钮
      labelWidth: 100,

      /**
       * 弹窗表单（与原型一致）
       */
      group: [
        {
          column: {
            // 1) 上级类目（只读展示）
            parentName: {
              label: "上级类目",
              span: 24,
              display: true, // 只在表单里显示
              addDisplay: true,
              editDisplay: true,
            },
            // 2) 类目名称（必填 ≤20 字）
            categoryName: {
              label: "类目名称",
              span: 24,
              placeholder: "请输入类目名称",
              type: "input",
              maxlength: 20,
              clearable: true,
              rules: [
                { required: true, message: "请输入类目名称", trigger: "blur" },
                { min: 1, max: 20, message: "最多 20 个字符", trigger: "blur" },
              ],
            },

            // 3) 类目排序（必填 正整数，越小排越前）
            sortNo: {
              label: "类目排序",
              span: 24,
              type: "number",
              controls: false,
              placeholder: "请输入排序，限制正整数，越小排越前",
              rules: [
                { required: true, message: "请输入类目排序", trigger: "blur" },
                {
                  validator: (_r, v, cb) => (/^[0-9]\d*$/.test(String(v)) ? cb() : cb(new Error("请输入正整数"))),
                  trigger: "blur",
                },
              ],
            },

            // 4) 适用对象（必选，至少勾选一项；保存时已在页面 hook 里拼接为逗号）
            applicableObjects: {
              label: "适用对象",
              span: 24,
              type: "checkbox",
              dicData: [], // ← 页面里动态填
              dicProps: { label: "label", value: "value" },
              rules: [
                { required: true, type: "array", message: "请至少选择一个适用对象", trigger: "change" },
                {
                  validator: (_r, v, cb) =>
                    Array.isArray(v) && v.length > 0 ? cb() : cb(new Error("请至少选择一个适用对象")),
                  trigger: "change",
                },
              ],
              // dicUrl: "/support-backend/dict/listChildrenByDictCode",
              // dicQuery: { dictCode: "qingyun.support.serviceCenter.applicableObject" }, // 按文档拼写
              // dicFormatter: res => {
              //   const list = Array.isArray(res) ? res : [];
              //   return list.map(i => ({ label: i.dictLabel, value: i.dictValue }));
              // },

              // rules: [
              //   { required: true, type: "array", message: "请至少选择一个适用对象", trigger: "change" },
              //   {
              //     validator: (_r, v, cb) =>
              //       Array.isArray(v) && v.length > 0 ? cb() : cb(new Error("请至少选择一个适用对象")),
              //     trigger: "change",
              //   },
              // ],
            },
          },
        },
      ],

      /**
       * 列表列
       */
      column: {
        // 隐藏字段：parentId（“加下级”时由页面赋值）
        parentId: { label: "父级ID", display: false, addDisplay: false, editDisplay: false, hide: true },

        categoryName: { label: "类目名称", minWidth: 160, display: false },

        sortNo: { label: "排序", width: 120, type: "number", display: false },

        applicableObjectsName: {
          label: "适用对象",
          minWidth: 140,
          addDisplay: false,
          editDisplay: false,
        },

        contentNum: { label: "内容数量", width: 110, addDisplay: false, editDisplay: false },

        createTime: {
          label: "新增时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },

        updateTime: {
          label: "最近编辑时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },

        updateName: {
          label: "操作人员",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
        },

        displayStatus: {
          label: "展示状态",
          minWidth: 120,
          display: false, // 列表里你用自定义 slot + 开关，所以这里不渲染内置
          dicData: [
            { label: "隐藏", value: 0 },
            { label: "展示", value: 1 },
          ],
        },
      },
    })
  );

  return { searchOption, tableOption };
};
